package cn.zyx.org.controller;

import cn.zyx.basic.util.AjaxResult;
import cn.zyx.basic.util.PageList;
import cn.zyx.org.domain.SystemDictionary;
import cn.zyx.org.domain.SystemDictionaryDetail;
import cn.zyx.org.query.SystemDictionaryDetailQuery;
import cn.zyx.org.query.SystemDictionaryQuery;
import cn.zyx.org.service.ISystemDictionaryDetailService;
import cn.zyx.org.service.ISystemDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    /**
     * 添加和修改
     * @param systemDictionary
     */
    @PutMapping
    public AjaxResult save(@RequestBody SystemDictionary systemDictionary){
        try {
            if (systemDictionary.getId() == null){ // 添加
                systemDictionaryService.add(systemDictionary);
            }else { // 修改
                systemDictionaryService.update(systemDictionary);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常!" + e.getMessage());
        }
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            systemDictionaryService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常!" + e.getMessage());
        }
    }

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public SystemDictionary getById(@PathVariable("id") Long id){
        return systemDictionaryService.getById(id);
    }

    /**
     * 分页查询数据
     * @param systemDictionaryQuery
     * @return
     */
    @PostMapping
    public PageList<SystemDictionary> pageData(@RequestBody SystemDictionaryQuery systemDictionaryQuery){
        return systemDictionaryService.pageData(systemDictionaryQuery);
    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            systemDictionaryService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常!" + e.getMessage());
        }
    }

    /**
     * 查询字段的详细信息
     * @param systemDictionaryDetailQuery
     * @return
     */
    @PostMapping("/detail")
    public PageList<SystemDictionaryDetail> getDetailById(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery){
        return systemDictionaryDetailService.getDetailById(systemDictionaryDetailQuery);
    }
}
